﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// GetFileHandler 的摘要说明
    /// </summary>
    public class GetFileHandler : IHttpHandler
    {
        #region 属性

        /// <summary>
        /// 系统编号
        /// </summary>
        private string SysCode{set;get;}
        
        /// <summary>
        /// 功能编号
        /// </summary>
        private string FuncCode{set;get;}
        
        /// <summary>
        /// 文件名 也有可能是Guid
        /// </summary>
        private string FileName{set;get;}

        private HttpContext Context { set; get; }

        #endregion

        public void ProcessRequest(HttpContext context)
        {
            Context = context;
            SysCode = context.Request.QueryString["sysCode"];
            FuncCode = context.Request.QueryString["funcCode"];
            FileName = context.Request.QueryString["fileName"];

            if (String.IsNullOrEmpty(FileName))
            {
                return;
            }

            FuncRoute();
        }

        /// <summary>
        /// 功能路由
        /// </summary>
        private void FuncRoute()
        {
            string getType = Context.Request.QueryString["gettype"] ?? "";////获取类型【first,last(默认)】
            switch (getType)
            {
                case "first": GetFirstFile(); break;
                case "last": 
                default:
                    GetLastFile(); break;
            }
        }

        /// <summary>
        /// 根据文件名获取最新文件
        /// </summary>
        private void GetLastFile()
        {
            Guid tmp;
            
            ////去掉后缀名后的字符串是36位Guid格式
            if ((FileName.Length == 36 || FileName.IndexOf('.') == 36) 
                && Guid.TryParse(FileName.Substring(0, 36), out tmp))
            {
                string guidStr = tmp.ToString();
                using (FileService.FileWebServiceSoapClient client = new FileService.FileWebServiceSoapClient())
                {
                    FileService.ArrayOfString files = client.GetFileList(guidStr, SysCode, FuncCode);
                    if (files != null && files.Count > 0)
                    {
                        FileName = files.Max();
                    }
                    GetFirstFile();
                }
            }
            else////即 文件名是Guid与时间的组合
            {
                GetFirstFile();
            }
        }

        /// <summary>
        /// 根据文件名获取第一个文件
        /// </summary>
        private void GetFirstFile()
        {
            FileService.FileWebServiceSoapClient fileService = new FileService.FileWebServiceSoapClient();
            byte[] file = fileService.DownFirstImageBytes(FileName, SysCode, FuncCode);

            if (file == null || file.Length == 0)
            {
                Context.Response.Write("/images/photobg.jpg");
            }
            else
            {
                Context.Response.ContentType = "image/*";
                Context.Response.BinaryWrite(file);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}